package org.kpy;

import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

public class MyClient {
    public static void main(String[] args) throws IOException {
        Socket socket=new Socket("localhost",6666);
        //向服务器发送数据
        OutputStream os=socket.getOutputStream();
        OutputStreamWriter osw=new OutputStreamWriter(os);
        BufferedWriter bw=new BufferedWriter(osw);
        //确定要发送的数据
        MyUser myUser=new MyUser("数据库",6789);
        //转换成JSON格式
        String jsonObject = JSONObject.toJSONString(myUser);
        bw.write(jsonObject+"\n");
        bw.flush();
        socket.shutdownOutput();


        InputStream is=socket.getInputStream();
        InputStreamReader isr=new InputStreamReader(is);
        BufferedReader br=new BufferedReader(isr);
        String str=null;
        while((str=br.readLine())!=null) {
            System.out.println("服务端返回数据："+str);
        }
        bw.close();
        br.close();
        socket.close();
    }
}